package cn.calm.osgi.admin.bundle.actions;

import java.io.File;
import java.net.MalformedURLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.osgi.interceptor.BundleContextAware;
import org.apache.struts2.osgi.interceptor.ServiceAware;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

import cn.calm.osgi.admin.bundle.entity.BundleInfo;
import cn.calm.osgi.admin.bundle.service.IBundleService;
import cn.calm.osgi.admin.bundle.util.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class AutoStartAction extends ActionSupport implements
		BundleContextAware, ServiceAware<IBundleService> {
	private static final Logger log=Logger.getLogger(AutoStartAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IBundleService service;
	private List<BundleInfo> autoStartBundles;
	private List<BundleInfo> notAutoStartBundles;
	private List<String> autoStarts;
	private BundleContext bundleContext;

	@Override
	public String execute() throws Exception {
		log.debug("index start");
		autoStartBundles = service.listAllAutoStartBundle();
		notAutoStartBundles = service.listAllNotAutoStartBundle();
		log.debug("index end");
		return super.execute();
	}

	public String update() throws Exception {
		log.debug("update start");
		service.clearAutoStart();
		int index = 0;
		for (String s : autoStarts) {
			String sp[] = s.split(";");
			service.addAutoStart(sp[0], sp[1], index);
			index++;
		}
		log.debug("update end");
		return execute();
	}

	public String start() throws Exception {
		log.debug("starting");
		String path = ServletActionContext.getServletContext().getRealPath(
				"/temp/");
		List<BundleInfo> list = service.listAllAutoStartBundleContent();
		for (BundleInfo info : list) {
			File file = new File(path + File.separator
					+ info.getBundleFilename());
			FileUtils.byteToFile(info.getContent(), file);
			log.info(file+"starting");
			try {
				Bundle b = bundleContext.installBundle(file.toURI().toURL()
						.toString());
				if (b != null) {
					b.start();
				}
				log.info(file+"started");
			} catch (BundleException e) {
				addActionError(e.getMessage());
				log.error(e);
			} catch (MalformedURLException e) {
				e.printStackTrace();
				log.error(e);
			}
		}
		log.debug("started");
		return "bundles";
	}

	@Override
	public void setServices(List<IBundleService> services) {
		if (services != null) {
			service = services.get(0);
		}
	}

	public List<BundleInfo> getNotAutoStartBundles() {
		return notAutoStartBundles;
	}

	public void setNotAutoStartBundles(List<BundleInfo> notAutoStartBundles) {
		this.notAutoStartBundles = notAutoStartBundles;
	}

	public List<BundleInfo> getAutoStartBundles() {
		return autoStartBundles;
	}

	public void setAutoStartBundles(List<BundleInfo> autoStartBundles) {
		this.autoStartBundles = autoStartBundles;
	}

	public List<String> getAutoStarts() {
		return autoStarts;
	}

	public void setAutoStarts(List<String> autoStarts) {
		this.autoStarts = autoStarts;
	}

	@Override
	public void setBundleContext(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}

}
